#include <iostream>
using namespace std;

namespace Protocol {
	#pragma pack(push)
	#pragma pack(4)
	class MsgHeader{
	public:
		MsgHeader(int type=-1):msg_type(type){}
		int msg_type;
	};
	#pragma pack(pop)
};

namespace Protocol {
	enum {
		MSG_FUCK_CS = 1,
	};
};

namespace Protocol {
	class CSFuck{
	public:
		CSFuck();
		MsgHeader header;
		int data;
	};


	CSFuck::CSFuck():header(MSG_FUCK_CS){}
};

namespace Protocol {
	class MsgHandler{
	public:
		MsgHandler();
		void OnRecv(const char* msg, int length);

	protected:
		typedef (MsgHandler::*HandlerFunc)(void* msg);
		static const int MAX_MSG_TYPE = 0xffff + 1;
		struct HandlerItem{
			HandlerItem(HandlerFunc f=0, int s=0):func(f), data_size(s){}
			HandlerFunc func;
			int data_size;
		};
		HandlerItem m_msg_handler_list[MAX_MSG_TYPE];
		
	public:
		void OnFuck(void* msg);
	};

	MsgHandler::MsgHandler(){
		m_msg_handler_list[MSG_FUCK_CS] = HandlerItem(&MsgHandler::OnFuck, sizeof(CSFuck));
	}

	void MsgHandler::OnRecv(const char* msg, int length){
		Protocol::MsgHeader* header = (Protocol::MsgHeader*)msg;
		int msg_type = header->msg_type;
		if (msg_type < 0 || msg_type >= MAX_MSG_TYPE)
			return;
		if (m_msg_handler_list[msg_type].func == 0)
			return;
		if (m_msg_handler_list[msg_type].data_size != length)
			return;

		(this->*m_msg_handler_list[msg_type].func)((void*)msg);
	}


	void MsgHandler::OnFuck(void* msg){
		Protocol::CSFuck* fuck = (Protocol::CSFuck*)msg;
		// ...
	}
};



int main() {
	// your code goes here
	return 0;
}







///////////////////////////////////////////////

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <event.h>
using namespace std;

// 事件base
struct event_base* base;

// 读事件回调函数
void onRead(int iCliFd, short iEvent, void *arg)
{
	int iLen;
	char buf[1500];

	iLen = recv(iCliFd, buf, 1500, 0);

	if (iLen <= 0) {
		cout << "Client Close" << endl;

		// 连接结束(=0)或连接错误(<0)，将事件删除并释放内存空间
		struct event *pEvRead = (struct event*)arg;
		event_del(pEvRead);
		delete pEvRead;

		close(iCliFd);
		return;
	}

	buf[iLen] = 0;
	cout << "Client Info:" << buf << endl;
}

// 连接请求事件回调函数
void onAccept(int iSvrFd, short iEvent, void *arg)
{
	int iCliFd;
	struct sockaddr_in sCliAddr;

	socklen_t iSinSize = sizeof(sCliAddr);
	iCliFd = accept(iSvrFd, (struct sockaddr*)&sCliAddr, &iSinSize);

	// 连接注册为新事件 (EV_PERSIST为事件触发后不默认删除)
	struct event *pEvRead = new event;
	event_set(pEvRead, iCliFd, EV_READ|EV_PERSIST, onRead, pEvRead);
	event_base_set(base, pEvRead);
	event_add(pEvRead, NULL);
}

int main()
{
	int iSvrFd;
	struct sockaddr_in sSvrAddr;

	memset(&sSvrAddr, 0, sizeof(sSvrAddr));
	sSvrAddr.sin_family = AF_INET;
	sSvrAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	sSvrAddr.sin_port = htons(8888);

	// 创建tcpSocket（iSvrFd），监听本机8888端口
	iSvrFd = socket(AF_INET, SOCK_STREAM, 0);
	bind(iSvrFd, (struct sockaddr*)&sSvrAddr, sizeof(sSvrAddr));
	listen(iSvrFd, 10);

	// 初始化base
	base = event_base_new();

	struct event evListen;
	// 设置事件
	event_set(&evListen, iSvrFd, EV_READ|EV_PERSIST, onAccept, NULL);
	// 设置为base事件
	event_base_set(base, &evListen);
	// 添加事件
	event_add(&evListen, NULL);

	// 事件循环
	event_base_dispatch(base);

	return 0;
}
